package com.tech.connect.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ksy.common.dialog.ItemsDialog;
import com.ksy.common.utils.ParamsUtil;
import com.ksy.oss.OssClient;
import com.ksy.oss.OssResult;
import com.tech.connect.R;
import com.tech.connect.api.AuthHttp;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.ImageApi;
import com.tech.connect.model.ItemCatSub;
import com.tech.connect.util.CatUtils;
import com.tech.connect.view.ImageAdder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AuthSkillActivity extends BaseActivity {

    private TextView tvCat;
    private EditText etNum;
    private TextView tvDes;
    private ImageAdder imageAdder;
    private ItemCatSub selectCat;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth_card_or_skill);
        getHeadBar().setTitle("认证中心");
        initView();
        loadData();
    }


    private void initView() {
        tvCat = findViewById(R.id.tvCat);
        tvCat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final List<ItemCatSub> catListByCode = CatUtils.getInstance().getCatListByCode("cert-type");
                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<ItemCatSub>(activity, catListByCode, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        dialog.dismiss();
                        ItemCatSub itemCatSub = catListByCode.get(position);
                        selectCat = itemCatSub;
                        tvCat.setText(selectCat.description);
                        tvDes.setText(selectCat.description + "的照片");
                    }
                });
                itemsDialog.show();
            }
        });
        etNum = findViewById(R.id.etNum);
        tvDes = findViewById(R.id.tvDes);
        imageAdder = findViewById(R.id.imageAdder);
        findViewById(R.id.btEnter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectCat == null) {
                    showToast("请选择证件类型");
                    return;
                }
                String trim = etNum.getText().toString().trim();
                if (isEmpty(trim)) {
                    showToast("请输入证件号码");
                    return;
                }
                List<String> images = imageAdder.getImages();
                if (images.isEmpty()) {
                    showToast("请选择证件照片");
                    return;
                }
                final Map<String, Object> map = AuthHttp.getMap();
                map.put("certName", selectCat.description);
                map.put("certNo", trim);
                showDialog();
                List<ParamsUtil.ParamsTwo<OssClient.FileType, String>> twoList = new ArrayList<>();
                for (String image : images) {
                    twoList.add(ParamsUtil.getInstance().create(OssClient.FileType.Image, image));
                }
                OssClient.getInstance().update(activity, twoList, new OssClient.UpdatesListener() {
                    @Override
                    public void onFinish(final boolean isSuccess, List<OssResult> results, final List<String> ossUrls) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (isSuccess) {
                                    map.put("images", ImageApi.getImageParams(ossUrls));
                                    update(map);
                                } else {
                                    showToast("图片上传失败");
                                    hideDialog();
                                }
                            }
                        });

                    }
                });
            }
        });
        imageAdder.bindContext(activity, 6);
    }

    private void update(Map<String, Object> map) {
        AuthHttp.authSkill(map, new BaseEntityOb<Object>() {
            @Override
            public void onDataDeal(boolean isSuccess, Object data, String msg) {
                hideDialog();
                showToast(msg);
                setResult(RESULT_OK);
                destroyActivity();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (imageAdder != null) {
            imageAdder.onImagePickResult(requestCode, resultCode, intent);
        }
    }

    private void loadData() {

    }

}